//
//  SearchPostVC.m
//  sensen_ios
//
//  Created by pro on 16/3/7.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "SearchPostVC.h"
#import "PostBrowseCell.h"
#import "PostDetailVC.h"
@interface SearchPostVC ()<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>
{
    UITableView *_tableView; // 表视图
    NSInteger _page; // 页码
    NSString *_searchName; // 搜索帖子关键字
    MJRefreshAutoNormalFooter *_footer;
}

@property (nonatomic, strong) NSMutableArray *itemsAry; // 普通帖子数组
@end

@implementation SearchPostVC
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSearchBar]; // 中间的搜索栏
    [self createLeftBarButtonItemWithImagename:nil]; // 左侧返回按钮
    [self _createTableView];//创建搜索表视图
    _itemsAry = [NSMutableArray arrayWithCapacity:1];

    [self _getData];
    
       // 下拉加载更多
    _footer= [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(_getMoreData)];
    _tableView.footer = _footer;

}
- (void)viewWillAppear:(BOOL)animated {
    
    // 隐藏标签栏
    MainTabBarController *main = (MainTabBarController *)self.tabBarController;
    [main hideTabBar];
}
- (void)viewWillDisappear:(BOOL)animated {
    
    // 显示标签栏
    MainTabBarController *main = (MainTabBarController *)self.tabBarController;
    [main showTabBar];
}
#pragma mark - 创建视图
#pragma mark -- 创建导航栏视图 --
// 创建中间的搜索栏
- (void)createSearchBar {
    UIView *searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 40 * 2, 35)];
    searchView.backgroundColor = SS_NAVI_BLUE_LIGHT_COLOR;
    searchView.layer.cornerRadius = searchView.height / 2.0;
    
    // 搜索
    UIImageView *searchImgView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 8, searchView.height - 16, searchView.height - 16)];
    searchImgView.image = [UIImage imageNamed:@"商城首页_搜索"];
    [searchView addSubview:searchImgView];
    
    // 搜索栏输入框
    UITextField *searchBarTf = [[UITextField alloc] initWithFrame:CGRectMake(searchImgView.right + 5, 2, searchView.width - searchImgView.width * 2 - searchView.height - 10, 31)];
    searchBarTf.tintColor = [UIColor whiteColor];
    searchBarTf.textColor = [UIColor whiteColor];
    searchBarTf.returnKeyType = UIReturnKeySearch;
    searchBarTf.delegate =self;
    NSDictionary *stringAttr = @{NSFontAttributeName:B_FONT(15),NSForegroundColorAttributeName:[UIColor whiteColor]};
    NSAttributedString *palceholderString = [[NSAttributedString alloc] initWithString:@"输入关键字搜索帖子" attributes:stringAttr];
    searchBarTf.attributedPlaceholder = palceholderString;
    [searchView addSubview:searchBarTf];
    
    self.navigationItem.titleView = searchView;
}


// 创建表视图
- (void)_createTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 15 ) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    // 注册单元格
    [_tableView registerClass:[PostBrowseCell class] forCellReuseIdentifier:@"PostBrowseCell"];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _itemsAry.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    PostBrowseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PostBrowseCell"];
    // 填充数据
    if (cell == nil) {
        cell = [[PostBrowseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PostBrowseCell"];
    }
    PostCellFrameLayout *layout = _itemsAry[indexPath.section];
    cell.layout = layout;
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    PostCellFrameLayout *layout = _itemsAry[indexPath.section];
    return layout.cellRect.size.height;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}
// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
// 分割线距离左边距离
-(void)viewDidLayoutSubviews
{
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        PostDetailVC *vc = [[PostDetailVC alloc] init];
        PostCellFrameLayout *layout = _itemsAry[indexPath.section];
        vc.tid = layout.model.tid;
        vc.fid = layout.model.fid;
        [self.navigationController pushViewController:vc animated:YES];
}

- (void)_getMoreData{
    _page = _page + 1; //每次刷新 请求的页的页数编号增加
    [self _pushData];
}
- (void)_pushData{
    
    NSDictionary *params;
    NSString *pageSize = @"5";
    NSString *pageNo = [NSString stringWithFormat:@"%ld",(long)_page];
    if (_searchName.length == 0) {
//        params= @{@"page_size": pageSize , @"page_no" : pageNo ,@"name" : @""};
        return;
    }else{
        params= @{@"page_size": pageSize , @"page_no" : pageNo ,@"name" :_searchName};
    }
   [BoyeHttpClient post:@"BY_ForumThread_query" apiVerson:100 params:params success:^(AFHTTPRequestOperation *operation, id result) {
       if (![result[@"list"] isKindOfClass:[NSNull class]]) {
           NSArray *array = result[@"list"];
           NSString *nub = result[@"count"];
           NSInteger nubInt = [nub intValue];
           for (NSInteger i = 0; i < array.count; i++)
           {
               NSDictionary *resultDic = array[i];
               PostModel *model = [[PostModel alloc] initContentWithDic:resultDic];
               PostCellFrameLayout *layout = [[PostCellFrameLayout alloc] init];
               layout.cellType = ZKCellBrowse;
               layout.model = model;
               [_itemsAry addObject:layout];
           }
           [_tableView reloadData];
           [_tableView.footer endRefreshing];
           if (_itemsAry.count == nubInt) {
               [_tableView.footer removeFromSuperview];
           }
       }else{
           [SVProgressHUD showOnlyStatus:@"没有相关帖子" withDuration:1.5];
           [_tableView reloadData];
       }
}
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"加载失败请重试" withDuration:1];
    }];

}
- (void)_getData{
    _page = 1;
    if (_itemsAry == nil) {
        _itemsAry = [[NSMutableArray alloc] init];
    }else{
        [_itemsAry removeAllObjects];
    }
    [self _pushData];
}

#pragma mark - 输入框代理事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    _searchName = textField.text;
    [self keyboardDown];
    [self _getData];
    
    return YES;
}
#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
